#!/bin/bash

#global variable

device=
bus_num=
dev_addr=
action=

#max9296_regs="0x13 0x1b 0x1d 0x1f 0x22 0x23 0x108 0x11a 0x12c 0x13e 0x103 0x115 0x127 0x139 0x541 0x542 0x1dc 0x1fc 0x21c 0x23c 0x55c 0x55d 0x55e 0x55f 0x42d 0x46d 0x4ad 0x4ed 0x50 0x51 0x52 0x53"

#isx031_frame_cnt_regs="0x7dc8 0x7dc9 0x7dca 0x7dcb"


max9296_96718_err_reg="0x13 0x1b 0x1d 0x1f 0x312"

max96718_video_lock_reg="0x11a 0x12c"

max96712_video_lock_reg="0x108 0x11a 0x12c 0x13e 0x150 0x168 0x17a 0x18c"

function help() {
cat <<EOF
$(basename $0) [-d] [-b] [-s] [-h]
-d|--serdes     -- device name
-b|--bus        -- device bus number
-s|--slave addr -- slave i2c addr
-c|--command    -- error, lock
-h|--help       -- show this help tips
EOF
exit 1
}

function read_default_reg() {
	bus_id=$1

	bus=$bus_id; dev=0x48; regs=$max9296_regs;
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done

	echo "--------serial 1---------------------------------------------------"
	bus=$bus_id; dev=0x41; regs="0x13 0x112 0x1f 0x341 0x342 0x343 0x344 0x6b 0x73 0x7b 0x83 0x93 0x98 0xa3 0xab 0x8b";
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done

	echo "--------serial 2---------------------------------------------------"
	bus=$bus_id; dev=0x42; regs="0x13 0x112 0x1f 0x341 0x342 0x343 0x344 0x6b 0x73 0x7b 0x83 0x93 0x98 0xa3 0xab 0x8b";
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done

	echo "--------sensor 1---------------------------------------------------"
	bus=$bus_id; dev=0x11; regs="0x2000 0x2001 0x2002 0x2003";
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done

	echo "--------sensor 2---------------------------------------------------"
	bus=$bus_id; dev=0x12; regs="0x2000 0x2001 0x2002 0x2003";
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done
}

function read_frame_counter() {

	bus_id=$1
	dev_id="0x41 0x42 0x43 0x44"
	
	for dev in $dev_id;
	do
		bus=$bus_num; regs=$isx031_frame_cnt_regs;
		for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done
	done
}

function 96718_read_lock() {
	
	bus=$bus_num; dev=$dev_addr; regs=$max96718_video_lock_reg;
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done
}

function 96712_read_lock() {

	bus=$bus_num; dev=$dev_addr; regs=$max96712_video_lock_reg;
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done
} 


function read_deserial_err()
{

	bus=$bus_num; dev=$dev_addr; regs=$max9296_96718_err_reg;
	for r in $regs; do echo "i2c$bus@$dev: $r = `i2ctransfer -y -f $bus w2@$dev $(($r >> 8)) $(($r & 0xff)) r1 `"; done
}


action_command=("error" "lock" "link_lock")

declare -A max9296_inst
max9296_inst[feat_error]=read_deserial_err


declare -A max96718_inst
max96718_inst[feat_error]=read_deserial_err
max96718_inst[feat_lock]=96718_read_lock

declare -A max96712_inst
max96712_inst[feat_error]=read_deserial_err
max96712_inst[feat_lock]=96712_read_lock

function param_parse() {
	ARGS=`getopt -o d:b:s:c:h --long dev:,bus:,slave:,cmd:,help -- "$@"`
	eval set -- "${ARGS}"

	while true
	do
		case "$1" in
			-d)
				echo "device:$2"
				device=$2
				shift 2
				;;
			-b)
				echo "bus_num:$2"
				bus_num=$2
				shift 2
				;;
			-s)
				echo "device addr:$2"
				dev_addr=$2
				shift 2
				;;
			-c)
				echo "to be action cmd ..."
				action=$2
				shift 2
				;;
			-h)
				help
				shift
				exit 0
				;;
			*)
				break
				;;
		esac
	done
}


function main() {
	param_parse $@

	if [ "$device" = "max9296" ]; then
		${max9296_inst[feat_err]}
	elif [ "$device" = "max96718" ]; then
		for command in "${action_command[@]}"
		do
			if [ $command = $action ]; then

				echo "-------- feat_$command"
				index="feat_$command"
				${max96718_inst[$index]}
			else
				continue
				echo "command not match !"
			fi
		done
	elif [ "$device" = "max96712" ]; then
    	for command in "${action_command[@]}"
			do
				if [ $command = $action ]; then
  					echo "-------- feat_$command"
					index="feat_$command"
					${max96712_inst[$index]}
				else
					continue
					echo "command not match !"	
				fi  
			 done
	fi
	exit 1
}

main $@
